import { Entity, Column, PrimaryGeneratedColumn, OneToMany, CreateDateColumn, UpdateDateColumn } from 'typeorm';
import { UserRole } from './user-role.entity';
import { TimeTransformer } from 'src/utils/TimeTransformer.util';
import { DEFAULT_PASSWORD } from 'src/common/constants';

/**
 * 用户实体
 */
@Entity('sys_user')
export class User {
  /**
   * 用户id
   */
  @PrimaryGeneratedColumn({ comment: '用户id' })
  userId: number;

  /**
   * 账号
   */
  @Column({ comment: '账号' })
  username: string;

  /**
   * 名称
   */
  @Column({ comment: '名称' })
  name: string;

  /**
   * 头像
   */
  @Column({ comment: '头像', nullable: true, type: 'text' })
  avatar: string;

  /**
   * 密码hash值
   */
  @Column({ comment: '密码hash值', default: DEFAULT_PASSWORD })
  password: string;

  /**
   * 邮箱
   */
  @Column({ comment: '邮箱', nullable: true })
  email: string;

  /**
   * 状态
   */
  @Column({ comment: '状态', default: true })
  status: Boolean;

  /**
   * 备注
   */
  @Column({ comment: '备注' })
  remark: string;

  /**
   * 创建时间
   */
  @CreateDateColumn({ comment: '创建时间', transformer: TimeTransformer })
  createTime: Date;

  /**
   * 更新时间
   */
  @UpdateDateColumn({ comment: '更新时间', transformer: TimeTransformer })
  updateTime: Date;

  /**
   * 用户角色
   */
  @OneToMany(() => UserRole, (userRole) => userRole.user)
  userRoles: UserRole[];
}
